package com.android.tea;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.android.tea.shopper.ShopperActivity;

public class BaseTabActivity extends TabActivity {

	private static TabHost tabHost;

	private LinearLayout myTab;
	private static View[] tabs;
	private static TextView[] titles;
	private static ImageView[] tabIcons;
	private static FrameLayout[] items;
	private static Resources resources;
	private String[] tags;
	OnClickListener tabOnClicklistener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.base_tab_layout);
		
		TeaEncyclopediaApp.list.add(this);
		
		resources = getResources();

		initLayout();
		setCurrentTab(0);

	}

	public static void setCurrentTab(int index) {
		onTabChange(index);

	}

	@SuppressWarnings("unchecked")
	private void initLayout() {

		tabHost = this.getTabHost();

		int[] tabTextResIds = new int[] { R.string.BaseTabActivity_Home,
				R.string.BaseTabActivity_Message,
				R.string.BaseTabActivity_Shop,
				R.string.BaseTabActivity_ShopMall,
				R.string.BaseTabActivity_Setting };

		int[] tabIconResIds = new int[] { R.drawable.tab_house,
				R.drawable.tab_news, R.drawable.tab_people,
				R.drawable.tab_shopping, R.drawable.tab_setup };

		Class[] tabIntentClasses = new Class[] { MainActivity.class,
				TeaMessageActivity.class, ShopperActivity.class,
				ShopActivity.class, SettingActivity.class, };

		tags = new String[tabTextResIds.length];

		for (int i = 0; i < tabTextResIds.length; i++) {
			tags[i] = getResources().getString(tabTextResIds[i]);
		}

		items = new FrameLayout[tabTextResIds.length];
		tabs = new View[tabTextResIds.length];
		titles = new TextView[tabTextResIds.length];
		tabIcons = new ImageView[tabTextResIds.length];
		myTab = (LinearLayout) findViewById(R.id.mytabs);
		tabOnClicklistener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				int tabIndex = v.getId();
				tabHost.setCurrentTabByTag(tags[tabIndex]);
				onTabChange(tabIndex);
			}
		};

		createTabs(tabTextResIds, tabIconResIds, tags, tabIntentClasses, myTab);
		items[0].setBackgroundResource(R.drawable.tab_click_black);// 背景设置
		tabIcons[0].setImageResource(R.drawable.tab_house_click);

	}

	protected static void onTabChange(final int tabIndex) {
		for (int i = 0; i < tabs.length; i++) {
			tabs[i].setBackgroundColor(0);
		}

		tabIcons[0].setImageResource(R.drawable.tab_house);
		tabIcons[1].setImageResource(R.drawable.tab_news);
		tabIcons[2].setImageResource(R.drawable.tab_people);
		tabIcons[3].setImageResource(R.drawable.tab_shopping);
		tabIcons[4].setImageResource(R.drawable.tab_setup);

		titles[0].setTextColor(resources.getColor(R.color.tab_default));
		titles[1].setTextColor(resources.getColor(R.color.tab_default));
		titles[2].setTextColor(resources.getColor(R.color.tab_default));
		titles[3].setTextColor(resources.getColor(R.color.tab_default));
		titles[4].setTextColor(resources.getColor(R.color.tab_default));
		if (tabIndex == 0) {
			items[0].setBackgroundResource(R.drawable.tab_click_black);
			tabIcons[0].setImageResource(R.drawable.tab_house_click);
		} else if (tabIndex == 1) {
			// tabs[1].setBackgroundColor(Color.parseColor("#bcd7aa"));
			items[1].setBackgroundResource(R.drawable.tab_click_black);
			tabIcons[1].setImageResource(R.drawable.tab_news_click);
		} else if (tabIndex == 2) {
			// tabs[2].setBackgroundColor(Color.parseColor("#bcd7aa"));
			items[2].setBackgroundResource(R.drawable.tab_click_black);
			tabIcons[2].setImageResource(R.drawable.tab_people_click);
		} else if (tabIndex == 3) {
			// tabs[3].setBackgroundColor(Color.parseColor("#bcd7aa"));
			items[3].setBackgroundResource(R.drawable.tab_click_black);
			tabIcons[3].setImageResource(R.drawable.tab_shopping_click);
		} else if (tabIndex == 4) {
			// tabs[4].setBackgroundColor(Color.parseColor("#bcd7aa"));
			items[4].setBackgroundResource(R.drawable.tab_click_black);
			tabIcons[4].setImageResource(R.drawable.tab_setup_click);
		}
	}

	@SuppressWarnings("unchecked")
	private void createTabs(int[] tabTextResIds, int[] tabIconResIds,
			String[] tags, Class[] tabIntentClasses, LinearLayout myTab) {
		for (int i = 0; i < tabTextResIds.length; i++) {
			View view = View.inflate(BaseTabActivity.this,
					R.layout.base_tab_item, null);
			FrameLayout tabItem = (FrameLayout) view
					.findViewById(R.id.tab_item);
			tabItem.setBackgroundResource(R.drawable.tab_click_gradient);
			items[i] = tabItem;

			TextView textView = (TextView) view
					.findViewById(R.id.tab_textview_title);
			textView.setText(getResources().getString(tabTextResIds[i]));
			textView.setTextColor(getResources().getColor(R.color.tab_default));

			titles[i] = textView;
			ImageView iv = ((ImageView) view
					.findViewById(R.id.tab_imageview_icon));
			iv.setImageResource(tabIconResIds[i]);
			tabIcons[i] = iv;

			Intent tabIntent = new Intent(this, tabIntentClasses[i]);
			TabHost.TabSpec spec = tabHost.newTabSpec(tags[i]).setIndicator(
					tags[i]).setContent(tabIntent);
			tabHost.addTab(spec);

			view.setLayoutParams(new LayoutParams(TeaEncyclopediaApp
					.getScreenWidth() / 5, LayoutParams.WRAP_CONTENT));
			myTab.addView(view);
			view.setOnClickListener(tabOnClicklistener);
			view.setId(i);
			tabs[i] = view;
		}
	}

	@Override
	public void onBackPressed() {
		new AlertDialog.Builder(this).setTitle("退出").setIcon(
				android.R.drawable.ic_dialog_info).setMessage("确认推出茶百科吗？")
				.setPositiveButton(R.string.confirm,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,int which) {
								TeaEncyclopediaApp.exit();
							}
						}).setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								
							}
						}).show();
	}

}
